﻿using System;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using ECube.Web.CMS.Extensions;
namespace ECube.Web.CMS.Models
{
    public class Widget
    {

        public Widget()
        {
            Family = "基础部件";
            Title = Family + "1";
            ViewName = "Widget";
        }

        public Widget(string id,string name) : this()
        {
            Id = id;
            Title = name;
        }
        public string Id { get; set; }
        public string Title    { get; set; }
        public string ViewName { get; set; }
        public string Content  { get; set; }
        public string type     { get; set; }  //完全限定类型名，用于序列化。
        public string Family { get; set; }

        public bool IsDesign { get; set; } //用于运行时
        public bool IsEdit { get; set; }

        public virtual void Render(HtmlHelper htmlHelper, bool isDesign,bool isEdit)
        {
            this.IsDesign = isDesign;
            this.IsEdit = isEdit;
            this.RenderContent(htmlHelper,ViewName); 

        }
        public virtual void RenderContent(HtmlHelper htmlHelper,string viewName)
        {
            if (String.IsNullOrEmpty(viewName))
            {
                if (IsEdit)
                {
                    htmlHelper.Write("<textarea>");
                    htmlHelper.Write(this.Content);
                    htmlHelper.Write("</textarea>");
                }
                else
                {
                    htmlHelper.Write(htmlHelper.Encode(this.Content));
                }
            }
            else
            {
                htmlHelper.RenderPartial(viewName, this);
            }
        }     

       
    }
}
